<template>
    <div class="login">
        <img src="../assets/img/phone.jpg" class="image" alt="">
        <section>
            <div class="box">
                <div class="form-box">
                    <button @click="toLogin" class="jmp"><h1 class=" jmp jmp-Login" :class="{'title':showLogin}">
                        登录</h1></button>
                    <button @click="toRegister" class="jmp"><h1 class="jmp jmp-Register"
                                                                :class="{'title':showRegister}">注册</h1></button>

                    <!-- 注册 -->
                    <form name="form_Register" v-if="showRegister" class="register-box" method="post"
                          onsubmit="return false;">
                        <div class="inputBox">
                            <input id="registerUsername" type="text" placeholder="用户名" name="registerUsername"
                                   v-model="registerUsername">
                        </div>

                        <div class="inputBox">
                            <input id="registerPassword" type="password" placeholder="密码" v-model="registerPassword"
                                   minlength="6" maxlength="18">
                            <div id="pwdPrompt">
                                <div id="pwdLength">
                                    <span>弱</span> <span>中</span><span>强</span>
                                </div>
                            </div>
                        </div>
                        <div class="inputBox">
                            <input id="phoneNumber" type="text" placeholder=手机号 name="registerPhone"
                                   v-model="registerPhone">
                            <p id="phoneNumber_err" style="display: none;color: red">手机号格式错误！</p>
                        </div>

                        <div class="inputBox">
                            <div class="email">
                                <input v-model="registerEmail" type="email" placeholder="邮箱" name="registerEmail">
                                <button @click="sendEmail()" class="captcha" :disabled=disabled>
                                    {{ sendEmailBtnTime }}{{ sendEmailBtn }}
                                </button>
                            </div>
                            <p id="email_err" style="display: none;color: red">邮箱格式错误！</p>
                        </div>
                        <div class="inputBox">
                            <input id="verifyCode" type="text" placeholder=验证码 v-model="verifyCode">
                            <p id="verifyCode_err" style="display: none;color: red">验证码错误！</p>
                        </div>
                        <button id="bt_Register" type="submit" @click="register()">注册</button>
                    </form>

                    <!-- 登录 -->
                    <form class="login-box" method="post" v-if="showLogin" action="login" onsubmit="return false;">
                        <input id="loginUsername" type="text" placeholder="用户名" name="loginUsername "
                               v-model="loginUsername">
                        <input id="loginPassword" type="password" placeholder="密码" name="loginPassword"
                               v-model="loginPassword">
                        <input type="text" v-model="codeInput" placeholder="请输入正确的计算结果">
                        <Captcha style="display: inline-block;width: 80%" @result-changed="onResultChanged"
                                 :codeInput="codeInput" ref="captcha"/>
                        <button @click="login()">登录</button>
                    </form>
                </div>
            </div>
        </section>
    </div>
</template>

<script>
import axios from "axios";
import Captcha from "@/components/Captcha.vue";

export default {
    name: "Login",
    components: {Captcha},
    data() {
        return {
            showLogin: true,
            showRegister: false,
            loginUsername: "",
            loginPassword: "",
            registerUsername: "",
            registerPassword: "",
            registerEmail: '',
            registerPhone: '',
            verifyCode: '',
            url: "",
            //生成验证码使用
            num1: 0,
            num2: 0,
            num3: 0,
            num4: 0,
            operator1: '+',
            operator2: '+',
            operator3: '+',
            result: 0,
            codeInput: '',
            isShowError: false,
            isConfirmPassword: false,
            sendEmailBtn: '发送验证码',
            sendEmailBtnTime: '',
            disabled: false
        }
    },
    methods: {
        onResultChanged(newVal) {
            // 在这里更新A组件中的变量
            this.result = newVal
        },
        toRegister() {
            this.showLogin = false;
            this.showRegister = true;
        },
        toLogin() {
            this.showLogin = true;
            this.showRegister = false;
        },

        login() {
            const captcha = this.$refs.captcha;
            if (captcha.verify()) {
                axios({
                    method: "post",
                    url: "/tokens",
                    params: {
                        username: this.loginUsername,
                        password: this.loginPassword,
                    }
                }).then((result) => {
                    let token = result.data.data.token
                    let status = result.data.status
                    if (status === 0) {
                        window.sessionStorage.setItem("username", this.loginUsername)
                        window.sessionStorage.setItem("token", token)
                        this.$router.go(-1)
                    } else {
                        this.$message({
                            message: result.data.message,
                            type: 'error'
                        });
                    }
                }).catch((error) => {
                    // console.log(error.response.data.message)
                    // this.error = error.response.data.message
                })
            } else {
                this.$message({
                    message: '验证码错误',
                    type: 'error'
                });
            }

        },

        //发送验证码
        sendEmail() {
            let email = this.registerEmail
            axios({
                url: '/emails',
                method: "post",
                data: {
                    email: email
                },
                headers: {'Content-Type': 'application/x-www-form-urlencoded'}
            }).then(res => {
                console.log(res)
                this.disabled = true
                this.sendEmailBtnTime = 5
                this.sendEmailBtn = '秒后重发'
                this.countDown()
            })
        },
        countDown() {
            const time = this.sendEmailBtnTime
            if (time > 0) {
                this.sendEmailBtnTime -= 1
                setTimeout(() => {
                    this.countDown()
                }, 1000)
            } else {
                this.sendEmailBtn = '发送验证码'
                this.sendEmailBtnTime = ''
                this.disabled = false
            }

        },
        register() {
            axios({
                method: 'post',
                url: '/users',
                data: {
                    username: this.registerUsername,
                    password: this.registerPassword,
                    phone: this.registerPhone,
                    email: this.registerEmail,
                    emailCode: this.verifyCode
                },
            }).then(res => {
                let status = res.data.status
                if (status === 100) {
                    this.$message({
                        message: res.data.message,
                        type: 'success'
                    });
                    this.loginUsername = this.registerUsername
                    this.loginPassword = this.registerPassword
                    this.toLogin()

                } else {
                    this.$message({
                        message: res.data.message,
                        type: 'error'
                    });
                }
            })
        },

    }
}
</script>

<style scoped>
body {
    background-color: #F7F7F7;
    height: 100%;
}

/* 使用flex布局，让内容垂直和水平居中 */

.login {
    margin: 0 auto;
    height: 100%;
    width: 100%;
}

img {
    position: absolute;
    vertical-align: middle;
    display: block;
    width: 100%;
    height: 100%;
    border: 0;
}

.title {
    color: #999999 !important;
}

.image {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    /*object-fit: cover;*/
}

section {
    position: absolute;
    top: 74px;
    right: 5%;
    background-color: #ffffff;

}

/* 登录框样式 */
.form-box {
    width: 400px;
    border-radius: 5px;

    /* box-shadow: 5px 5px 5px rgba(0, 0, 0, 0.1); */
}

/* 登录标题样式 */
.form-box h1 {
    display: inline;
    width: 50%;
    position: relative;
    color: rgba(0, 0, 0, 0.615);
    font-size: 28px;
    font-weight: 600;
    letter-spacing: 5px;
    padding-left: 90px;
    cursor: pointer;
}

/* 登录标题的下划线样式 */

.form-box h1::before {
    content: "";
    position: absolute;
    left: 90px;
    bottom: -10px;
    width: 0;
    height: 3px;
    background: #586FFF;
    transition: 0.5s;
}

.form-box h1:hover:before {
    width: 60px;
}


.form .inputBox {
    width: 100%;
    margin-top: 20px;
}

/* 输入框样式 */
.form .inputBox input {
    width: 100%;
    padding: 10px 20px;
    background: rgba(238, 12, 12, 0.2);
    border-radius: 30px;
    font-size: 16px;
    color: rgb(81, 161, 84);

}

.form .inputBox input::placeholder {
    color: rgb(62, 148, 148);
}

/* 登录按钮样式 */

.form .inputBox input[type="submit"] {
    background: #fff;
    color: rgb(171, 56, 56);
    max-width: 100px;
    margin-bottom: 20px;
    font-weight: 600;
    cursor: pointer;
}

.forget {
    margin-top: 6px;
    color: #fff;
    letter-spacing: 1px;
}

.forget a {
    color: #fff;
    font-weight: 600;
    text-decoration: none;
}

.form-box {
    left: 5%;
    background-color: #ffffff;
    padding-top: 2em;
    border-radius: 5px;
    /* box-shadow: 2px 0 10px rgba(0, 0, 0, 0.1); */
    /* 动画过渡 加速后减速 */
}

.register-box, .login-box {
    /* 弹性布局 垂直排列 */
    padding-top: 2.5em;
    display: flex;
    flex-direction: column;
    align-items: center;
    width: 100%;
}

.hidden {
    display: none;
}

img.judge {
    width: 20px;
    height: 20px;
}

h1 {
    text-align: center;
    /* 大写 */
    text-transform: uppercase;
    color: #fff;
    /* 字间距 */
    letter-spacing: 5px;
}


.inputBox input {
    background-color: transparent;
    width: 80%;
    color: #00006B;
    border: 1.6px solid #DEDFE0;
    padding: 10px 0;
    text-indent: 10px;
    margin: 8.5px 0;
    font-size: 14px;
    letter-spacing: 2px;
}

input {
    background-color: transparent;
    width: 80%;
    color: #00006B;
    border: 1.6px solid #DEDFE0;
    padding: 10px 0;
    text-indent: 10px;
    margin: 8.5px 0;
    font-size: 14px;
    letter-spacing: 2px;
}

input::placeholder {
    color: #AAAAAA;
}

input:focus {
    color: #00006B;
    transition: 0.5s;
}

input:focus::placeholder {
    opacity: 0;
}

.inputBox {
    width: 100%;
    padding-left: 20%;
}

.border-Color {
    border-color: red !important;
}

#pwdLength span {
    display: none;
    float: left;
    height: 14px;
    width: 106px;
    font-size: 12px;
    text-align: center;
    line-height: 14px;
    color: white;
    border-right: 1px solid white;
    border-radius: 5px;
}

.jmp {
    text-align: center;
    border: none;
    background-color: #ffffff;
}


.form-box button:not(.jmp):not(.captcha) {
    width: 80%;
    margin-top: 25px;
    margin-bottom: 30px;
    font-size: 16px;
    background-color: #f6f6f6;
    outline: none;
    border-radius: 5px;
    padding: 13px;
    background-color: #638aef;
    color: #fff;;
    letter-spacing: 2px;
    border: none;
    cursor: pointer;
}

.form-box button:hover:not(.jmp):not(.captcha) {
    background-color: #5766FF;
    color: #f6f6f6;
    transition: background-color 0.5s ease;
}

.email {
    height: 40px;
    border-radius: 8px;
    padding: 0 10px 14.7px 0;
}

.email input {
    width: 320px;
}

.email button {
    background-color: #ffffff;
    border: none;
    cursor: pointer;
}

.captcha {
    position: absolute;
    left: 70%;
    color: #707DFF;
    margin-top: 4.7%;
    font-size: 14px;
}

.captcha:hover {
    color: #3078DC;
}

.con-box {
    width: 50%;
    /* 弹性布局 垂直排列 居中 */
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    /* 绝对定位 居中 */
    position: absolute;


}


.con-box h2 {
    color: #8e9aaf;
    font-size: 25px;
    font-weight: bold;
    letter-spacing: 3px;
    text-align: center;
    margin-bottom: 4px;
}

.con-box p {
    font-size: 12px;
    letter-spacing: 2px;
    color: #8e9aaf;
    text-align: center;
}

.con-box span {
    color: #d3b7d8;
}

.con-box img {
    width: 150px;
    height: 150px;
    opacity: 0.9;
    margin: 40px 0;
}

.con-box button {
    margin-top: 3%;
    background-color: #fff;
    color: #a262ad;
    border: 1px solid #d3b7d8;
    padding: 6px 10px;
    border-radius: 5px;
    letter-spacing: 1px;
    outline: none;
    cursor: pointer;
}

.con-box button:hover {
    background-color: #d3b7d8;
    color: #fff;
}

.verify {
    margin-left: 18%;
    width: 100%;
    padding: 10px 20px;
    position: relative;

}

.verify input {
    width: 16em;

}

.verify button {
    width: auto;
    position: absolute;
    top: -1.5em;
    right: 5em;
    height: 3em;
    border: none;
    outline: none;
}


</style>